package zj.com.activities;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import zj.com.adapter.GsSelectAdapter;
import zj.com.app.ZjApplication;
import zj.com.model.InspectItem;
import zj.com.model.JcxDB;
import zj.com.model.Org;
import zj.com.net.NetManager;
import zj.com.net.OnResponseListener;
import zj.com.util.DialogUtil;
import zj.com.zj.R;

/**
 * Created by xi on 16/6/29.
 */
public class GsSelectActivity extends Activity implements View.OnClickListener{

    public ListView listView;
    public TextView title_name;
    public ImageView left_iv;
    public TextView right_tv;

    public List<Org> list;
    public GsSelectAdapter adapter;

    public boolean isMutiSelect = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_gs_select);
        left_iv = (ImageView) findViewById(R.id.left_iv);
        left_iv.setOnClickListener(this);
        left_iv.setVisibility(View.VISIBLE);


        right_tv = (TextView) findViewById(R.id.right_tv);
        listView = (ListView) findViewById(R.id.listView);
        title_name = (TextView) findViewById(R.id.title_name);
        title_name.setText("选择");
        list = new ArrayList<>();
        adapter =new GsSelectAdapter(list,this);
        listView.setAdapter(adapter);

        int type = getIntent().getIntExtra("type",0);

        isMutiSelect = getIntent().getBooleanExtra("isMutiSelect",false);

        if(isMutiSelect){

            right_tv.setVisibility(View.VISIBLE);
            right_tv.setText("确定");
            right_tv.setOnClickListener(this);
        }

        if(type == 1){
            Org gs = (Org) getIntent().getSerializableExtra("org");

            if(gs == null){

                Toast.makeText(this, "请先选择公司", Toast.LENGTH_SHORT).show();
            }else{

                dialog = DialogUtil.createProgressDialog(this,"提示","获取数据中...");
                NetManager.getInstance(this).queryBm(gs.getOrgCode(), new OnResponseListener<List<Org>>() {
                    @Override
                    public void onComplete(boolean isOK, List<Org> result, int errorCode, String errorMsg) {

                        if(isOK){

                            list = result;
                            adapter.setDataChange(list);

                        }else{
                            Toast.makeText(GsSelectActivity.this, "获取失败", Toast.LENGTH_SHORT).show();

                        }
                        dialog.dismiss();
                    }

                    @Override
                    public void onInternError(int errorCode, String errorMessage) {
                        Toast.makeText(GsSelectActivity.this, "获取失败", Toast.LENGTH_SHORT).show();
                        dialog.dismiss();
                    }
                });
            }

        }else{
            obtainData();
        }

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                if(isMutiSelect){


                    adapter.setSelect(position);

                }else{
                    Org org =    list.get(position);
                    Intent intent = new Intent();
                    intent.putExtra("org",org);
                    setResult(RESULT_OK,intent);
                    finish();
                }


            }
        });

    }

    public ProgressDialog dialog;
    public void obtainData(){

        dialog = DialogUtil.createProgressDialog(this,"提示","数据获取中...");
        NetManager.getInstance(this).QueryOrgs(new OnResponseListener<List<Org>>() {
            @Override
            public void onComplete(boolean isOK, List<Org> result, int errorCode, String errorMsg) {

                if(isOK){
                    list = result;
                    adapter.setDataChange(result);
                }else{
                    Toast.makeText(GsSelectActivity.this, "获取失败", Toast.LENGTH_SHORT).show();

                }
                dialog.dismiss();
            }

            @Override
            public void onInternError(int errorCode, String errorMessage) {

                Toast.makeText(GsSelectActivity.this, "获取失败", Toast.LENGTH_SHORT).show();
                dialog.dismiss();
            }
        });
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()){
            case R.id.left_iv:
                finish();
                break;
            case R.id.right_tv:

                doSelect();
                break;
        }

    }

    public  void doSelect(){

     Map<Integer,Org> map =   adapter.getSelectMap();
        List<Org> list = new ArrayList<>();
        for(Map.Entry<Integer, Org> entry:map.entrySet()){

            Org org  = entry.getValue();
            list.add(org);
        }

        Intent intent = new Intent();
        intent.putExtra("org",(Serializable)list);
        setResult(RESULT_OK,intent);
        finish();
    }
}
